package com.aifast.common.util;

import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author fujian
 * @since 2022/1/19 15:08
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class SideMenu implements Serializable {
    private static final long serialVersionUID = 7200282306785433080L;

    private Integer id;
    private String name;
    private String icon;
    private String url;
    private Integer pid;
    private List<SideMenu> subMenus = new ArrayList<>();

    public static List<SideMenu> init(List<SideMenu> list){
        List<SideMenu> sideMenus = new ArrayList<>();
        if(ObjectUtil.isNull(list) || list.size() == 0){
            return sideMenus;
        }
        list.forEach(i->{
            if(ObjectUtil.equal(0,i.getPid()) || ObjectUtil.isNull(i.getPid())){
                sideMenus.add(i);
                return;
            }
            list.forEach(j->{
                if(ObjectUtil.equal(j.getId(),i.getPid())){
                    j.getSubMenus().add(i);
                    return;
                }
            });

        });

        return sideMenus;
    }

}

